<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h3 class="title">中医药文化知识科普系统</h3>
        <div class="divider"></div>
      </div>
      <el-form class="login-form" label-position="left">
        <!-- 用户名输入 -->
        <el-form-item class="form-item">
          <span class="svg-container">
            <svg-icon icon-class="user" class="icon" />
          </span>
          <el-input
            placeholder="请输入用户名"
            v-model="rulesForm.username"
            class="custom-input"
          />
        </el-form-item>

        <!-- 密码输入 -->
        <el-form-item class="form-item">
          <span class="svg-container">
            <svg-icon icon-class="password" class="icon" />
          </span>
          <el-input
            placeholder="请输入密码"
            type="password"
            v-model="rulesForm.password"
            class="custom-input"
          />
        </el-form-item>

        <!-- 角色选择 -->
        <el-form-item class="role-select">
          <el-radio-group v-model="rulesForm.role">
            <el-radio
              v-for="item in roleOptions"
              :key="item.value"
              :label="item.value"
              class="role-radio"
            >
              {{ item.key }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- 登录按钮 -->
        <div class="action-group">
          <el-button type="primary" @click="login()" class="login-btn">
            立即登录
          </el-button>
          <el-link 
            type="primary" 
            class="register-link"
            :underline="false"
            @click="register(roleOptions[0]?.value)"
          >
            没有账号？立即注册
          </el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import menu from "@/utils/menu";

export default {
  data() {
    return {
      rulesForm: {
        username: "",
        password: "",
        role: "",
        code: "",
      },
      menus: [],
      roleOptions: [],
      tableName: "",
      codes: [
        {
          num: 1,
          color: "#000",
          rotate: "10deg",
          size: "16px",
        },
        {
          num: 2,
          color: "#000",
          rotate: "10deg",
          size: "16px",
        },
        {
          num: 3,
          color: "#000",
          rotate: "10deg",
          size: "16px",
        },
        {
          num: 4,
          color: "#000",
          rotate: "10deg",
          size: "16px",
        },
      ],
    };
  },
  mounted() {
    let menus = menu.list();
    this.menus = menus;
    for (let i = 0; i < this.menus.length; i++) {
      if (this.menus[i].hasBackLogin == "是") {
        let menuItem = {};
        menuItem["key"] = this.menus[i].roleName;
        menuItem["value"] = this.menus[i].tableName;
        this.roleOptions.push(menuItem);
      }
    }
  },
  created() {
    this.getRandCode();
  },
  methods: {
    menuChange(role) {},
    register(tableName) {
      this.$storage.set("loginTable", tableName);
      this.$router.push({ path: "/register" });
    },
    // 登陆
    login() {
      let code = "";
      for (let i in this.codes) {
        code += this.codes[i].num;
      }
      if ("0" == "1" && !this.rulesForm.code) {
        this.$message.error("请输入验证码");
        return;
      }
      if (
        "0" == "1" &&
        this.rulesForm.code.toLowerCase() != code.toLowerCase()
      ) {
        this.$message.error("验证码输入有误");
        this.getRandCode();
        return;
      }
      if (!this.rulesForm.username) {
        this.$message.error("请输入用户名");
        return;
      }
      if (!this.rulesForm.password) {
        this.$message.error("请输入密码");
        return;
      }
      if (this.roleOptions.length > 1) {
        console.log("1");
        if (!this.rulesForm.role) {
          this.$message.error("请选择角色");
          return;
        }
        let menus = this.menus;
        for (let i = 0; i < menus.length; i++) {
          if (menus[i].tableName == this.rulesForm.role) {
            this.tableName = menus[i].tableName;
            this.rulesForm.role = menus[i].roleName;
          }
        }
      } else {
        this.tableName = this.roleOptions[0].value;
        this.rulesForm.role = this.roleOptions[0].key;
      }
      this.$http({
        url: `${this.tableName}/login?username=${this.rulesForm.username}&password=${this.rulesForm.password}`,
        method: "post",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$storage.set("Token", data.token);
          this.$storage.set("userId", data.userId);
          this.$storage.set("role", this.rulesForm.role);
          this.$storage.set("sessionTable", this.tableName);
          this.$storage.set("adminName", this.rulesForm.username);
          this.$router.replace({ path: "/index/" });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    getRandCode(len = 4) {
      this.randomString(len);
    },
    randomString(len = 4) {
      let chars = [
        "a",
        "b",
        "c",
        "d",
        "e",
        "f",
        "g",
        "h",
        "i",
        "j",
        "k",
        "l",
        "m",
        "n",
        "o",
        "p",
        "q",
        "r",
        "s",
        "t",
        "u",
        "v",
        "w",
        "x",
        "y",
        "z",
        "A",
        "B",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "I",
        "J",
        "K",
        "L",
        "M",
        "N",
        "O",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "U",
        "V",
        "W",
        "X",
        "Y",
        "Z",
        "0",
        "1",
        "2",
        "2",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
      ];
      let colors = [
        "0",
        "1",
        "2",
        "2",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "a",
        "b",
        "c",
        "d",
        "e",
        "f",
      ];
      let sizes = ["14", "15", "16", "17", "18"];

      let output = [];
      for (let i = 0; i < len; i++) {
        // 随机验证码
        let key = Math.floor(Math.random() * chars.length);
        this.codes[i].num = chars[key];
        // 随机验证码颜色
        let code = "#";
        for (let j = 0; j < 6; j++) {
          let key = Math.floor(Math.random() * colors.length);
          code += colors[key];
        }
        this.codes[i].color = code;
        // 随机验证码方向
        let rotate = Math.floor(Math.random() * 60);
        let plus = Math.floor(Math.random() * 2);
        if (plus == 1) rotate = "-" + rotate;
        this.codes[i].rotate = "rotate(" + rotate + "deg)";
        // 随机验证码字体大小
        let size = Math.floor(Math.random() * sizes.length);
        this.codes[i].size = sizes[size] + "px";
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 50%, #a5d6a7 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  position: relative;
  overflow: hidden;

  &::before {
    content: "";
    position: absolute;
    width: 200%;
    height: 200%;
    background: repeating-linear-gradient(
      45deg,
      rgba(255, 255, 255, 0.1) 25%,
      transparent 25%,
      transparent 50%,
      rgba(255, 255, 255, 0.1) 50%,
      rgba(255, 255, 255, 0.1) 75%,
      transparent 75%
    );
    animation: bgMove 20s linear infinite;
  }

  .login-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.98);
    width:580px;
    padding: 50px 60px;
    border-radius: 20px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
    transform: translateY(-5%);
    // transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    // position: relative;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.3);

    &:hover {
      transform: translateY(-7%);
      box-shadow: 0 15px 50px rgba(0, 0, 0, 0.2);
    }
  }
  .login-form {
    position: static;
    display: block!important;
  }
  .login-header {
    text-align: center;
    margin-bottom: 40px;

    .title {
      font-size: 28px;
      background: linear-gradient(45deg, #2d5d2d, #4caf50);
      -webkit-background-clip: text;
      background-clip: text;
      color: transparent;
      font-weight: 700;
      letter-spacing: 1.5px;
      text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
    }

    .divider {
      width: 80px;
      height: 3px;
      background: linear-gradient(
        90deg,
        transparent 0%,
        #6aaf6a 50%,
        transparent 100%
      );
      margin: 15px auto;
      transition: all 0.3s;

      &:hover {
        width: 120px;
      }
    }
  }
  .el-form-item--medium .el-form-item__content {
    display: flex!important;
    align-items: center;
  }
  .form-item {
    display: flex;
    align-items: center;
    margin-bottom: 28px;
    position: relative;

    .svg-container {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 46px;
      height: 46px;
      background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
      border-radius: 12px 0 0 12px;
      transition: all 0.3s;
      box-shadow: inset 2px 0 8px rgba(106, 175, 106, 0.1);

      .icon {
        width: 20px;
        height: 20px;
        color: #6aaf6a;
        filter: drop-shadow(1px 1px 1px rgba(0, 0, 0, 0.1));
      }
    }

    .custom-input {
      ::v-deep .el-input__inner {
        width: 313px;
        height: 46px;
        border-left: 0;
        border-radius: 0 12px 12px 0;
        background: rgba(232, 245, 233, 0.3);
        transition: all 0.3s;
        font-size: 15px;
        padding-left: 15px;

        &::placeholder {
          color: #9ccc9c;
        }

        &:focus {
          border-color: #8bc38b;
          box-shadow: 0 0 12px rgba(139, 195, 139, 0.3);
          background: rgba(232, 245, 233, 0.5);
        }
      }
    }
  }

  .role-select {
    margin: 25px 0;

    ::v-deep .el-radio-group {
      display: flex;
      justify-content: center;
      gap: 15px;
      width: 100%;
    }

    ::v-deep .el-radio {
      margin: 0;
      flex: 1;
      max-width: 120px;

      &.role-radio {
        .el-radio__label {
          display: block;
          width: 100%;
          padding: 8px 15px;
          border-radius: 8px;
          background: rgba(232, 245, 233, 0.3);
          border: 1px solid #c8e6c9;
          transition: all 0.3s;
          text-align: center;
        }

        .el-radio__input {
          display: none;
        }

        &.is-checked {
          .el-radio__label {
            background: linear-gradient(45deg, #6aaf6a, #8bc38b);
            color: white;
            border-color: #6aaf6a;
            box-shadow: 0 4px 12px rgba(106, 175, 106, 0.3);
          }
        }

        &:hover:not(.is-checked) {
          .el-radio__label {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(106, 175, 106, 0.2);
          }
        }
      }
    }
  }

  .login-btn {
    width: 100%;
    height: 48px;
    background: linear-gradient(45deg, #6aaf6a, #8bc38b);
    border: none;
    border-radius: 12px;
    font-size: 16px;
    letter-spacing: 1.5px;
    color: white;
    transition: all 0.3s;
    position: relative;
    overflow: hidden;

    &::before {
      content: "";
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(
        120deg,
        transparent,
        rgba(255, 255, 255, 0.3),
        transparent
      );
      transition: 0.5s;
    }

    &:hover {
      background: linear-gradient(45deg, #5a9f5a, #7bb37b);
      transform: translateY(-2px);
      box-shadow: 0 8px 20px rgba(106, 175, 106, 0.4);

      &::before {
        left: 100%;
      }
    }

    &:active {
      transform: translateY(0);
      box-shadow: 0 4px 12px rgba(106, 175, 106, 0.3);
    }
  }
}

@keyframes bgMove {
  0% {
    transform: rotate(0deg) translateX(-25%);
  }
  100% {
    transform: rotate(0deg) translateX(25%);
  }
}

@media (max-width: 768px) {
  .login-container {
    padding: 15px;

    .login-card {
      width: 100%;
      padding: 30px;
      border-radius: 16px;
    }

    .login-header .title {
      font-size: 24px;
    }
  }
}
</style>
